﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using WebClient;
namespace WebClient.admin.Modules.products
{
    public partial class AddNew : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
            this.btnCancel.Attributes.Add("onclick", "return ClearAll()");
            if (!this.IsPostBack) {
                LoadGroupProduct();
            }
        }

        private void LoadGroupProduct() {
            WebData db = new WebData();
            this.cbbgroupId.DataSource = db.GetAllGroupproducts();           
            this.cbbgroupId.DataValueField = "id";
            this.cbbgroupId.DataTextField = "name";
            this.cbbgroupId.DataBind();

            this.cbbVendorId.DataSource = db.GetAllVendor();
            this.cbbVendorId.DataValueField = "id";
            this.cbbVendorId.DataTextField = "name";
            this.cbbVendorId.DataBind();
        }
        private void AddNewItem()
        {
            long groupId = 1;
            int vendorId = 1;
            try {
                groupId = Convert.ToInt64(this.cbbgroupId.SelectedValue);
                vendorId = Convert.ToInt32(this.cbbVendorId.SelectedValue);
            }
            catch { }
            FileUploads file = new FileUploads();

            String sPath = "";
            sPath = file.FileSaveImage("/images/sanpham", this.pathImage);
            WebData data = new WebData();
            DTO.Products item = new DTO.Products();
           // item.id =Convert.ToInt64(this.txtid.Text.Trim());
            item.groupProductId = 1;// Convert.ToInt64(this);
            item.productName = this.txtproductName.Text.Trim();
            item.description = this.txtdescription.Text.Trim();
            item.create_on = DateTime.Now;
            item.price = Convert.ToDecimal(this.txtprice.Text.Trim());
            item.updatePriceLast = item.price;
            item.pathImage = sPath;
            item.content = this.txtcontent.Text;
            item.isActive = 1;
            item.vendorId = vendorId;
            data.OnSubmitProducts(item, 0);
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            AddNewItem();
        }
    }
}